package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.dto.SetmealDTO;
import com.reggie.dto.SetmealPageQueryDTO;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.exception.DeletionNotAllowedException;
import com.reggie.exception.SetmealEnableFailedException;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.SetmealDishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.result.PageResult;
import com.reggie.service.SetmealService;
import com.reggie.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 套餐分页功能
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO pageQueryDTO) {

        //设置分页参数
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        //调用mapper实现分页查询
        Page<SetmealVO> page = setmealMapper.pageQuery(pageQueryDTO);
        //返回分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {

        //根据套餐id查询套餐
        SetmealVO setmealVO = setmealMapper.findById(id);

        //根据套餐id查询setmealDish
        List<SetmealDish> setmealDishes = setmealDishMapper.findbyId(id);

        //封装成setmealVO
        setmealVO.setSetmealDishes(setmealDishes);
        //返回数据
        return setmealVO;
    }

    /**
     * 套餐的起售和禁售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //调用mapper
        //根据套餐id查询菜品的id
        List<Long> ids = setmealDishMapper.findDishById(id);
        if (status == StatusConstant.ENABLE){
            //判断套餐是否和菜品关联，
            if (ids != null && ids.size() > 0){
                // 如果关联，在启用套餐的时候判断关联的套餐是否是禁用中，
                for (Long dishId : ids) {
                    Dish dish = dishMapper.findById(dishId);
                    // 如果是禁用中，这抛出业务异常
                    if (dish.getStatus() == StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        setmealMapper.startOrStop(status,id);
    }

    /**
     * 套餐新增
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void sace(SetmealDTO setmealDTO) {
        //新建setmeal对象,进行属性拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //调用mapper实现添加、
        setmealMapper.add(setmeal);

        //将setmealDish中的属性全部填满
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //添加菜品
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //新建一个setmeal，用来修改setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //修改套餐表
        setmealMapper.update(setmeal);

        //根据套餐id删除菜品
        setmealDishMapper.delete(setmealDTO.getId());

        //然后添加页面传过来的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.addSetmealDish(setmealDishes);
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {

        //获取套餐数据
        ids.forEach(id -> {
            SetmealVO setmeal = setmealMapper.findById(id);
            //判断套餐是否是起售中
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                //如果起售则不能删除,抛出业务异常
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        for (Long id : ids) {
            //删除setmeal表数据
            setmealMapper.deleteBatch(id);
            //删除setmealdish表数据
            setmealDishMapper.delete(id);
        }

    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<SetmealVO> findCategory(Integer categoryId) {

        List<SetmealVO> setmealVOList = new ArrayList<>();

        //根据分类id查询套餐
        List<Setmeal> setmealList = setmealMapper.findCategory(categoryId);
        for (Setmeal setmeal : setmealList) {

            Long id = setmeal.getId();
            List<SetmealDish> setmealDishes = setmealDishMapper.findbyId(id);

            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal,setmealVO);
            setmealVO.setSetmealDishes(setmealDishes);

            setmealVOList.add(setmealVO);
        }
        return setmealVOList;
    }

}
